This example demonstrates how to manage content items in Pyramid.
The example uses API authentication driven from JavaScript. See Authentication APIs for alternatives.
var pyramidURL = "http://mySite.com/api2/";
let token = callApi("auth/authenticateUser",{
"data":{
"userName":"adminUser",
"password":"abc123!"
}
},false);
log("got token "+token);
let defaultTenant=callApi("access/getDefaultTenant",{
"auth": token
}).data;
let tenentPublicFolder = callApi("content/getPublicOrGroupFolderByTenantId",{
"folderTenantObject": {
"validRootFolderType": 1,
"tenantId": defaultTenant
},
"auth": token
}).data;
let folderCreation = callApi("content/createNewFolder",{
"folderTenantObject": {
"parentFolderId": tenentPublicFolder.id,
"folderName": "new folder"
},
"auth": token
}).data;
let folderId=folderCreation.modifiedList[0].id;
let findRole = callApi("access/getRolesByName",{
"data": {
"searchValue": "role1",
"searchMatchType": 2
},
"auth": token
});
let roleId=findRole.data[0].roleId;
log("found role with id= "+ roleId);
let addRoleToFolder= callApi("content/addRoleToItem",{
"roleToItemApiData": {
"itemId": folderId,
"roleId": roleId
},
"auth": token
});
let file="http://myOtherSite.com/Sample.pie";
let pieFile=readPieFile(file);
let importContent = callApi("content/importContent",{
"pieApiObject": {
"rootFolderId": folderId,
"fileZippedData": pieFile,
"clashDefaultOption":1,
"rolesAssignmentType":3
},
"auth": token
}).data;
let itemId=Object.keys(importContent.importDscMap)[0];
let findContentItem = callApi("content/findContentItem",{
"searchParams":{
"searchString":"import",
"filterTypes":[3],
"searchMatchType":2,
"searchRootFolderType":1
},
"auth": token
}).data;
if(importContent.importDscMap[itemId][0].needsToPerformDsc){
step 10A: get item's data source connection
let correntConnectionStringId=importContent.importDscMap[itemId][0].connectionStringProperties.id
let changedDatasource = callApi("dataSources/changeDataSource",{
"dscApiData":{
"fromConnId":correntConnectionStringId,
"toConnId":"55ff277a-53ff-4c39-8f15-5651f1026a2d",
"itemId":itemId
},
"auth": token
});
}
let copyItems = callApi("content/copyItems",{
"moveItemsObject":{
"itemsForMove":[itemId],
"destinationFolder":tenentPublicFolder.id
},
"auth": token
}).data;
let removeRoleFromCreatedFolder= callApi("content/removeRolesFromItem",{
"rolesInItemRemovalObject": {
"itemId": folderId,
"roleIds": [roleId]
},
"auth": token
});
let folderDeletion = callApi("content/purgeContentItems",{
"itemIds":[folderCreation.modifiedList[0].id],
"auth": token
});
let itemSoftDeletion = callApi("content/softDeleteContentItems",{
"itemIds":[copyItems.modifiedList[0].id],
"auth": token
});
function log(msg){
document.write(msg);
console.log(msg);
}
function callApi(path,data,parseResult=true){
var xhttp = new XMLHttpRequest();
xhttp.open("POST", pyramidURL+path, false);
xhttp.send(JSON.stringify(data));
if(parseResult){
return JSON.parse(xhttp.responseText);
}else{
return xhttp.responseText;
}
}
function readPieFile(file){
var rawFile = new XMLHttpRequest();
rawFile.open("GET", file, false);
rawFile.send(null);
rawFile.onreadystatechange = function () {
if (request.readyState === 4 && request.status === 200) {
return request.responseText;
}
}
}